//
//  UIAlertController+Extensions.swift
//  MYModule
//
//  Created by 易先生 on 2022/10/31.
//

import Foundation

public extension UIAlertController{
    
    @discardableResult
    static func showAlert(title:String,message: String, in viewController: UIViewController) ->UIAlertController {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "dialog_confirm".language, style: .cancel))
        viewController.present(alert, animated: true)
        return alert
    }
    
    @discardableResult
    static func showAlertOk(title:String,message: String, in viewController: UIViewController, handler: ((UIAlertAction) -> Void)? = nil) ->UIAlertController {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        
        let action = UIAlertAction(title: "dialog_ok".language, style: .cancel, handler: handler)
        alert.addAction(action)
        
        viewController.present(alert, animated: true)
        return alert
    }
    
    @discardableResult
    static func showAlert(title:String,message: String) ->UIAlertController? {
        if let vc = UIApplication.shared.keyWindow?.rootViewController {
           return showAlert(title: title, message: message, in: vc)
        }
        return nil
    }
    
    @discardableResult
    static func showConfirm(title:String,message: String, in viewController: UIViewController,
                            confirm: ((UIAlertAction)->Void)?, cancel: (() -> ())? = nil) ->UIAlertController {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "page_cancel".language, style: .default, handler: { action in
            alert.dismiss(animated: true)
            cancel?()
        }))
        alert.addAction(UIAlertAction(title: "dialog_confirm".language, style: .default, handler: confirm))
        viewController.present(alert, animated: true)
        
        return alert
    }
    
    @discardableResult
    static func showEndTraining(title:String,message: String, in viewController: UIViewController,
                                confirm: ((UIAlertAction)->Void)?) ->UIAlertController {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "page_cancel".language, style: .default, handler: { action in
            alert.dismiss(animated: true)
        }))
        alert.addAction(UIAlertAction(title: "dialog_end".language, style: .default, handler: confirm))
        viewController.present(alert, animated: true)
        
        return alert
    }
    
    @discardableResult
    static func showConfirm(title:String,message: String, confirm: ((UIAlertAction)->Void)?, cancel: (() -> ())? = nil)->UIAlertController? {
        if let vc = UIApplication.shared.keyWindow?.rootViewController {
           return showConfirm(title: title, message: message, in: vc, confirm: confirm, cancel: cancel)
        }
        return nil
    }
    
    @discardableResult
    static func showPermissionSetting(title:String,message: String, in viewController: UIViewController, confirm: ((UIAlertAction)->Void)?, cancel: @escaping (() -> ())) ->UIAlertController {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "dialog_gps_training_not_turn_on_location_cancel".language, style: .default, handler: { action in
            alert.dismiss(animated: true)
            cancel()
        }))
        alert.addAction(UIAlertAction(title: "dialog_gps_training_not_turn_on_location_go_setting".language, style: .default, handler: confirm))
        viewController.present(alert, animated: true)
        
        return alert
    }
    
    static func showOkAlert(title:String,message: String, in viewController: UIViewController, ok: @escaping () -> ()) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "dialog_ok".language, style: .default, handler: { action in
            alert.dismiss(animated: true)
            ok()
        }))
        viewController.present(alert, animated: true)
    }

}
